﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq.Expressions;

namespace KinectAntiTheft_BL
{
    /// <summary>
    /// for i18n, please see
    /// http://www.codeproject.com/Articles/29495/Binding-and-Using-Friendly-Enums-in-WPF
    /// </summary>
    public enum Mode
    {
        Login = 1,
        Register = 2,
        ChangeSettings=3
    }
    public class User: IValidatableObject, IDataErrorInfo,INotifyPropertyChanged
    {
        Mode modeUser=Mode.Register;
        public Mode ModeUser
        {
            get { return modeUser; }
            set {
                modeUser = value;
                this.OnPropertyChanged(() => this.ModeUser);
                this.OnPropertyChanged(() => this.IsRegister);
                this.OnPropertyChanged(() => this.IsLogin);
            }
        }

        public void GuessModeUser()
        {
            
            if (string.IsNullOrWhiteSpace(this.UserEmail))
            {
                ModeUser = Mode.Login;
            }
            else
            {
                ModeUser = Mode.Register;
            }
        }





        public string userEmail;


        public string UserEmail
        {
            get { return userEmail; }
            set { userEmail = value;
            this.OnPropertyChanged(() => this.UserEmail);
            }
        }

        private string applicationGuid;
        public string ApplicationGuid
        {
            get { return applicationGuid; }
            set
            {
                applicationGuid = value;
                this.OnPropertyChanged(() => this.ApplicationGuid);
            }
        }
        
        
        
        
        private string userName;
        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                this.OnPropertyChanged(()=>this.UserName);
            }
        }

        string userPassword { get; set; }


        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; this.OnPropertyChanged(()=>this.UserPassword); }
        }


        



        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (string.IsNullOrEmpty(UserName))
                yield return new ValidationResult("UserName", new string[1] { "UserName" });


            if (string.IsNullOrEmpty(UserPassword))
                yield return new ValidationResult("UserPassword", new string[1] { "UserPassword" });

            if (IsRegister)
            {
                if (string.IsNullOrEmpty(UserEmail))
                    yield return new ValidationResult("UserEmail", new string[1] { "UserEmail" });
            }

        }




        public string Error
        {
            get { return this[""]; }
        }

        public string this[string columnName]
        {
            get
            {
                string ret = null;
                var data = this.Validate(new ValidationContext(this, null, null));
                switch(columnName)
                {
                    case "":
                        
                        foreach (var item in data)
                        {
                            ret += item.ErrorMessage + Environment.NewLine;
                        }
                        break;
                    default:
                        foreach (var item in data)
                        {
                            if(item.MemberNames.Contains(columnName) )
                                ret += item.ErrorMessage + Environment.NewLine;
                        }
                        break;
                }
                return ret;
                
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged<T>(Expression<Func<T>> exp)
        {
            var body = exp.Body as MemberExpression;
            OnPropertyChanged(body.Member.Name);

        }
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public bool IsRegister
        {
            get
            {
                return this.ModeUser == Mode.Register;
            }
        }
        public bool IsLogin
        {
            get
            {
                return !IsRegister;
            }
        }
    }
}
